package com.example.demo.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.CustomerMenu;
import com.example.demo.mapper.CustomerMenuMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/customermenu")
public class CustomerMenuController {

    @Resource
    CustomerMenuMapper customermenuMapper;

    @PostMapping
    public Result<?> save(@RequestBody CustomerMenu customermenu){
        customermenuMapper.insert(customermenu);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody CustomerMenu customermenu){
        customermenuMapper.updateById(customermenu);
        return Result.success();
    }

    @DeleteMapping("/{foodid}")
    public Result<?> update(@PathVariable long foodid){
        customermenuMapper.deleteById(foodid);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<CustomerMenu> wrapper = Wrappers.<CustomerMenu>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(CustomerMenu::getFoodid, search);
        }
        Page<CustomerMenu> customermenuPage = customermenuMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(customermenuPage);
    }
}
